//
//  AppSetting.m
//  AppFinaceHT
//
//  Created by LionGlory on 13-8-20.
//  Copyright (c) 2013年 Lion. All rights reserved.
//

#import "AppSetting.h"

#import "AFNetworking.h"

static AppSetting *appset = nil;

static float appScreenHeight = 0;

@implementation AppSetting

+(AppSetting*) manager{
    @synchronized(self){
        if (appset == nil) {
            appset = [[self alloc] init];
        }
    }
    return appset;
}

// 启动 默认值检查
-(void) launchingDefaultSetting{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"K_SET_NEWS_KUAI_XUN"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"K_SET_NEWS_KUAI_XUN"];
        
        [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"LIVE_REFRESH_HIGH"];
        [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"LIVE_REFRESH_MIDDLE"];
        [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"LIVE_REFRESH_LOW"];
        [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"IS_ACTIVE"];
        [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"Flage"];
    }
}

/*
 自定义试图高度
 ios7以下，去掉 状态栏20的高度
 ios7以上，不去掉 状态栏20的高度
 */
-(float)appScreenHeight{
    if (appScreenHeight != 0) {
        return appScreenHeight;
    }
    
    [self mainScreenSize];
    
    return appScreenHeight;
}

-(void)mainScreenSize{
    
    CGSize mainScreen = [[UIScreen mainScreen] bounds].size;
    
    appScreenHeight = mainScreen.height;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
        appScreenHeight -= 20;
    }

    NSLog(@" mainScreen height = %f", appScreenHeight);
}

// @isAuto YES，为启动时自动检查，无新版本时不需要提示信息, NO 为手动检查，有无新版本都需要提示


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (appUpdateType == 1) {
        if (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appUpdateUrl]];
        }
    } else if (appUpdateType == 3) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appUpdateUrl]];
    }
}



////////////////////////////////////////////////////////////////////////////////
#pragma mark - Server Time
////////////////////////////////////////////////////////////////////////////////
-(void)serverUnixTime{
    
    appUpdateType = 0;
    
    NSString *urlStr = @"http://htmmarket.fx678.com/unixtime.php";
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    
    [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSData *strData = responseObject;
        
        NSString *serverUnixTime =  [[NSString alloc]initWithData:strData encoding:NSUTF8StringEncoding];
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        long long int unixTime = (long long int)time;
        
        NSString *localUnixTime = [NSString stringWithFormat:@"%lld", unixTime];
        
        NSLog(@" localUnixTime = %@", localUnixTime);
        
        [[NSUserDefaults standardUserDefaults] setValue:serverUnixTime forKey:@"HT_SERVER_UNIXTIME"];
        [[NSUserDefaults standardUserDefaults] setValue:localUnixTime forKey:@"HT_LOCAL_UNIXTIME"];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [[NSUserDefaults standardUserDefaults] setValue:@"0" forKey:@"HT_SERVER_UNIXTIME"];
        [[NSUserDefaults standardUserDefaults] setValue:@"0" forKey:@"HT_LOCAL_UNIXTIME"];
        
    }];

}

-(NSDictionary*) httpUnixTime{
    NSString *HT_SERVER_UNIXTIME =  [[NSUserDefaults standardUserDefaults] objectForKey:@"HT_SERVER_UNIXTIME"];
    NSString *HT_LOCAL_UNIXTIME =  [[NSUserDefaults standardUserDefaults] objectForKey:@"HT_LOCAL_UNIXTIME"];
    
    long long int requestUnixTime = 0;
    
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    long long int nowUnixTime = (long long int)time;
    
    if ([HT_SERVER_UNIXTIME longLongValue] == 0) {
        requestUnixTime = nowUnixTime;
    } else {
        long long int localTimeOffset  = nowUnixTime - [HT_LOCAL_UNIXTIME longLongValue];
        requestUnixTime = [HT_SERVER_UNIXTIME longLongValue] + localTimeOffset;
    }
    
    NSString *httpUnixTime = [NSString stringWithFormat:@"%lld", requestUnixTime];
    
    int from = (int)[httpUnixTime length] - 6;
    NSString *httpUnixTimeKey = [httpUnixTime substringFromIndex:from];

    /*
    NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:8*3600];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970: requestUnixTime];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:GTMzone];
    [dateFormatter setDateFormat:@"dd-HH/mm:ss"];
    [dateFormatter stringFromDate:date];
    
    NSString *httpUnixTimeKey = [dateFormatter stringFromDate:date];
    */
    //NSLog(@"httpUnixTime = %@, httpUnixTimeKey = %@", httpUnixTime, httpUnixTimeKey);
    
    return @{@"time": httpUnixTime, @"time_key": httpUnixTimeKey};
}





@end
